﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace RESP_IIWaterAssessment
{
    public partial class CanalsForm : Form
    {
        tb_canal canal;
        tb_CanalState canalState;

        bool updaterec = true;
        int regID, avpID;
        int Year;

        TextBox tbCanal = new TextBox();


        public CanalsForm()
        {
            InitializeComponent();
        }

        private void cbRegions_SelectedIndexChanged(object sender, EventArgs e)
        {
            regID = Convert.ToInt32(cbRegions.SelectedValue);
            cbAVP.DataSource = UtilClass.FillAVP(regID);
            cbAVP.DisplayMember = "Name";
            cbAVP.ValueMember = "ID";
            cbAVP.SelectedIndex = -1;
            cbAVP.Text = string.Empty;
            cbAVP.Enabled = true;
        }

        private void CanalsForm_Load(object sender, EventArgs e)
        {
            if (InformationClass.isAdmin)
            {
                cbRegions.SelectedIndexChanged -= cbRegions_SelectedIndexChanged;
                cbRegions.DataSource = UtilClass.FillCBRegions();
                cbRegions.DisplayMember = "Name";
                cbRegions.ValueMember = "ID";
                cbRegions.SelectedIndex = -1;
                cbRegions.Text = string.Empty;
                cbRegions.SelectedIndexChanged += new EventHandler(cbRegions_SelectedIndexChanged);               
            }
            else
            {
                regID = InformationClass.regionID;
                cbRegions.Enabled = false;
                cbAVP.Enabled = true;
                cbAVP.DataSource = UtilClass.FillAVP(regID);
                cbAVP.DisplayMember = "Name";
                cbAVP.ValueMember = "ID";
                cbAVP.SelectedIndex = -1;
                cbAVP.Text = string.Empty;
            }
        }

        private void cbAVP_SelectedIndexChanged(object sender, EventArgs e)
        {
            avpID = UtilClass.GetAVPID(cbAVP.Text, regID);

            cbCanals.Enabled = true;
            cbCanals.Items.AddRange(UtilClass.GetCanals(avpID));
        }

        private void cbCanals_SelectedIndexChanged(object sender, EventArgs e)
        {
            groupBox2.Enabled = tbNote.Enabled = true;

            canal = DBClass.Canals.Where(wh => wh.Name == cbCanals.Items[cbCanals.SelectedIndex].ToString() && wh.AVPID == avpID)
                .SingleOrDefault();
            if (canal != null)
                ShowCanal();
        }

        private void ShowCanal()
        {
            canalState = DBClass.CanalState.Where(wh => wh.CanalID == canal.ID && wh.Date == DBClass.CanalState.Max(mx => mx.Date)).SingleOrDefault();
            tbBadLength.Text = canalState.BadLength.ToString();

            tbArea.Text = canal.Area.ToString();
            tbWaterUser.Text = canal.WaterUser.ToString();
            tbLength.Text = canal.Length.ToString();            
            tbNote.Text = canal.Note;
            cbCoverType.SelectedIndex = cbCoverType.Items.IndexOf(UtilClass.GetCategoryDisplayName((int)canal.CoverType, "CoverType"));

        }

        private void newCanalButton_Click(object sender, EventArgs e)
        {
            groupBox2.Enabled = tbNote.Enabled = true;
            groupBox1.Controls.Add(tbCanal);
            tbCanal.Location = cbCanals.Location;
            tbCanal.Size = cbCanals.Size;
            cbCanals.Visible = false;

            tbArea.Text = tbBadLength.Text = tbLength.Text = tbNote.Text = tbWaterUser.Text = string.Empty;

            updaterec = false;
        }

        private void buttonOK_Click(object sender, EventArgs e)
        {
            decimal area, length, badlength;
            int users;

            if (string.IsNullOrEmpty(cbCanals.Text) || string.IsNullOrEmpty(tbCanal.Text) || string.IsNullOrEmpty(tbLength.Text) ||
                string.IsNullOrEmpty(tbArea.Text))
            {
                MessageBox.Show("Не корректро введены данные");
                return;
            }

            Decimal.TryParse(tbArea.Text, out area);
            Decimal.TryParse(tbLength.Text, out length);
            Decimal.TryParse(tbBadLength.Text, out badlength);

            
            int.TryParse(tbWaterUser.Text, out users);
            

            if (updaterec)
            {
                canal.Area = area;
                //canal.BadLength = badlength;
                canal.Length = length;
                canal.Name = cbCanals.Text;
                canal.Note = tbNote.Text;
                canal.CoverType = UtilClass.GetCategoryValue("ChannelType", cbCoverType.Items[cbCoverType.SelectedIndex].ToString());

                //canalEquipment.Year = Year;
                //canalEquipment.BridgeNeed = bridge;
                //canalEquipment.ChannelNeed = chanelneed;
                //canalEquipment.ChannelQuantity = chanel;
                //canalEquipment.RailNeed = rail;
                //canalEquipment.SANIIRIQuantity = saniiri;
                //canalEquipment.SANIIRINeed = saniirineed;
                //canalEquipment.ShieldNeed = shield;
                //canalEquipment.WeirNeed = weirneed;
                //canalEquipment.WeirQuantity = weir;

            }
            else
            {
                canal = new tb_canal
                {
                    Name = tbCanal.Text,
                    Area = area,
                    AVPID = avpID,
                    //BadLength = badlength,
                    CoverType = UtilClass.GetCategoryValue("ChannelType", cbCoverType.Items[cbCoverType.SelectedIndex].ToString()),
                    Length = length,
                    Note = tbNote.Text
                };

                DBClass.RESPContext.tb_canals.InsertOnSubmit(canal);
                DBClass.RESPContext.SubmitChanges();

                //canalEquipment = new tb_equipment
                //{
                //    Year = this.Year,
                //    WeirQuantity = weir,
                //    WeirNeed = weirneed,
                //    ShieldNeed = shield,
                //    SANIIRINeed = saniirineed,
                //    SANIIRIQuantity = saniiri,
                //    RailNeed = rail,
                //    ChannelQuantity = chanel,
                //    ChannelNeed = chanelneed,
                //    CanalID = canal.ID,
                //    BridgeNeed = bridge
                //};

                //DBClass.RESPContext.tb_equipments.InsertOnSubmit(canalEquipment);
            }

            DBClass.RESPContext.SubmitChanges();
            updaterec = true;
        }

        private void numYear_ValueChanged(object sender, EventArgs e)
        {
            if (canal != null)
                ShowCanal();
        }

        private void deleteCanalButton_Click(object sender, EventArgs e)
        {
            if (canal != null)
            {
                DBClass.RESPContext.tb_canals.DeleteOnSubmit(canal);
                DBClass.RESPContext.SubmitChanges();
                this.Refresh();
            }
        }

        private void buttonCanalState_Click(object sender, EventArgs e)
        {
            CanalStateForm canState = new CanalStateForm();
            canState.ShowDialog();
        }
    }
}
